package filter;

import entity.User;
import service.UserService;
import utils.CookieUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter {

    private UserService userService = new UserService();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //获取请求的URI
        //URL:http://localhost:8080/meeting/add、http://localhost:8080/css/custom.css、http://localhost:8080/user/login
        //URL:/meeting/add、/css/custom.css、/user/login
        String requestURI = request.getRequestURI();

        // add、custom.css、login
        String targetName = requestURI.substring(requestURI.lastIndexOf("/")+1);


        if(
                requestURI.startsWith("/bootstrap")||
                requestURI.startsWith("/css")||
                requestURI.startsWith("/fonts")||
                requestURI.startsWith("/img")||
                requestURI.startsWith("/js")||
                requestURI.startsWith("/upload")||
                requestURI.startsWith("/vendor")||

                targetName.equals("login")||
                targetName.equals("register")||
                targetName.equals("checkUsername")||
                targetName.equals("checkEmail")||
                targetName.equals("wx_login")||
                targetName.equals("getCode")||
                targetName.equals("resetUser")||

                targetName.equals("checkCode.jsp")||
                targetName.equals("taglib.jsp")||
                targetName.equals("forget.jsp")||
                targetName.equals("index.jsp")||
                targetName.equals("register.jsp")

        ){
            //这些都是不受限资源，直接放行
            filterChain.doFilter(request, response);
            return;

        }

        //受限资源，要判断这个用户是否登录了
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");


        if(loginUser != null){
            filterChain.doFilter(request, response);
            return;
        }

        //校验cookie
        Cookie userIdCookie = CookieUtils.getCookie(request, "userId");

        if(userIdCookie != null){
            //如果session里面没有用户信息，那就查询cookie
            Long userId = Long.parseLong(userIdCookie.getValue());
            User user =userService.findById(userId);

            //手动存入session
            session.setAttribute("loginUser", user);
            filterChain.doFilter(request, response);
            return;

        }


        //受限资源，而且还没有登录(session里面没有用户信息，cookie里面也没有用户信息)-->登录
        response.sendRedirect("/index.jsp");

    }

    @Override
    public void destroy() {

    }
}
